package com.iwunu.gateway.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import com.alibaba.fastjson.JSONObject;
import com.iwunu.gateway.service.LoginService;
import com.iwunu.gateway.vo.LoginParamVo;
import com.iwunu.iot.common.constant.ResultCodeEnum;
import com.iwunu.iot.common.util.Result;
import com.iwunu.iot.common.wechat.OpenIdUtil;
/**
 * @author kinfeng
 * @date 2021/6/13 11:26 AM
 * @description
 */
@CrossOrigin
@RequestMapping("/common")
@Controller
@Api(tags = "短信，登录接口", description = "短信，登录接口")
public class LoginController {
	private static final Logger LOGGER = LoggerFactory.getLogger(LoginController.class);
    @Autowired
    private LoginService loginService;

    @RequestMapping(value = "/sendsms",method= RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "发送短信验证码")
    public ResponseEntity sendSmsCode(
            @RequestParam(required = false,defaultValue = "86") String cc,
            @ApiParam(value = "手机号", required = true)@RequestParam String phone
    ){
        Result result = loginService.sendSmsCode(cc,phone);
        return new ResponseEntity<>(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/login",method= RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "手机号登录接口")
    public ResponseEntity login(
            @RequestBody LoginParamVo loginParamVo
    ){
        Result result = null;
		try {
			result = loginService.login(loginParamVo);
		} catch (Exception e) {
			e.printStackTrace();
		}
        return new ResponseEntity<>(result, HttpStatus.OK);
    }
	/**
	 * *获取OpenId
	 * @param param
	 * @return
	 */
    @RequestMapping(value = "/getOpenId/{code}" , method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public  ResponseEntity<Result> getOpenId(
			@ApiParam(value = "code", required = true) @PathVariable("code") String code
			) {
    	Result result = null ;
    	JSONObject resultJson = OpenIdUtil.oauth2GetOpenid(code);
    	if(resultJson.containsKey("openid")||resultJson.containsKey("session_key")){
           	 result = new Result(ResultCodeEnum.CMDISSUED_SUCCESS.getCode(), ResultCodeEnum.CMDISSUED_SUCCESS.getDesc(),resultJson);
           	 return new ResponseEntity<>(result, HttpStatus.OK);
    	 }
    	result = new Result(ResultCodeEnum.INVALID_PARAMS.getCode(), ResultCodeEnum.INVALID_PARAMS.getDesc(),null);
    	return new ResponseEntity<>(result, HttpStatus.OK);	 
	}
//    @RequestMapping(value = "/{type}/login/bindPhone",method= RequestMethod.POST)
//    @ResponseBody
//    @ApiOperation(value = "登录绑定手机号接口")
//    public ResponseEntity loginBindPhone(
//            @ApiParam(value = "授权的小程序类型 1.智联公寓", required = true , defaultValue = "1")@PathVariable("type") Integer type,
//            @RequestBody LoginBindPhoneVo loginBindPhoneVo
//    ){
//        Result result = loginService.loginBindPhone(type,loginBindPhoneVo);
//        return new ResponseEntity<>(result, HttpStatus.OK);
//    }
//
//    @RequestMapping(value = "/{type}/login/openId",method= RequestMethod.POST)
//    @ResponseBody
//    @ApiOperation(value = "获取openid接口，如果已存在并绑定手机号，直接登录返回token,否则可根据返回isExist字段判断是否需要绑定手机号，为0的时候需要")
//    public ResponseEntity getOpenId(
//            @ApiParam(value = "授权的小程序类型 1.智联公寓", required = true , defaultValue = "1")@PathVariable("type") Integer type,
//            @ApiParam(value = "登录获取的token值", required = true)@RequestHeader String code
//
//    ){
//        Result result = loginService.getOpenId(type,code);
//        return new ResponseEntity<>(result, HttpStatus.OK);
//    }

    @RequestMapping(value = "/refreshToken",method= RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "刷新token")
    public ResponseEntity refreshToken(
            @ApiParam(value = "登录获取的token值", required = true)@RequestHeader String Authorization
    ){
        Result result = loginService.refreshToken(Authorization);
        return new ResponseEntity<>(result, HttpStatus.OK);
    }



}
